Having trouble with if statement in lisp

Having trouble with if statement in lisp

Anonymous
Not applicable
1,077 Views
11 Replies
Message 1 of 12

Having trouble with if statement in lisp

Anonymous
Not applicable

 

 

I'm sure that this is vairly easy, but its my first lisp and I'm having trouble adding an if statement. I am modifying another lisp to suit my needs, what i have added is in red.

 

My main goal is to set an if statement (if (Selcted txt = "10'", "5'", if( selected txt = "20'", "10'", if(etc.)

 

Any help at all would be great.

 

 

(defun c:test (/ vpObj fldObj vpID util fldtxt)
  (vl-load-com)
  (setq vpObj  (vlax-ename->vla-object
   (car (nentsel "\nSelect the viewport: "))
        )
 fldObj (vlax-ename->vla-object (car (nentsel "\nSelect the Field: ")))
 vpID   (vla-get-objectid vpObj)
  );; setq
  (if (> (vl-string-search "x64" (getvar "platform")) 0)
    (progn
      (setq util (vla-get-Utility
     (vla-get-activedocument (vlax-get-acad-object))
   )
     vpID (vlax-invoke-method
     util
     "GetObjectIdString"
     vpObj
     :vlax-False
   )
      );; setq
    );; progn
    (setq vpID (vl-princ-to-string (vla-get-Objectid vpObj)))
  );; if
  (setq fldtxt (strcat "%<\\AcObjProp Object(%<\\_ObjId "
   vpID
   ">%).TextString>%"
  )
  )
  (vla-put-textstring fldObj fldtxt)
  (vla-update fldObj)
  (vla-regen (vla-get-activedocument (vlax-get-acad-object))
      1
  )
(setq gsfive "5'")
(setq gsten "10'")

 ;;if
  (= fldtxt gsten)
 (progn (gsfive)) ("ERROR")

  (princ)
);; test

0 Likes
1,078 Views
11 Replies
Replies (11)
Message 2 of 12

dbroad
Mentor
Mentor

(if (Selcted txt = "10'", "5'", if( selected txt = "20'", "10'", if(etc.)

 

The etc means you shouldn't be using an if.  Use cond instead:

(cond
   (( = txt "10'") 5)
   ((= txt "20'") 10)
   (T  "etc")
)
    

The cond function returns the appropriate value (5, 10, etc)

 

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 3 of 12

Kent1Cooper
Consultant
Consultant

Is it always, as in your example values, supposed to return half as much, in whole feet [no inches or fractions], and will the initial value always be an even number of whole feet so that dividing it in half will always result in a whole number of feet?  If so, you don't need to pre-define returned values for specific initial values as you seem to be trying to do, but can get half the initial value returned, whatever it is -- no (if) or (cond) functions necessary.  With the DIMZIN System Variable set to 0 or 2, do this:

 

(rtos (/ (distof fldtxt) 2) 4 0)

 

If fldtxt is "10'", that returns "5'"; if it's "20'", that returns "10'"; if it's "100'", that returns "50'"; if it's "1234'", that returns "617'"; etc., etc.

 

With some tweaking of settings and arguments, the same approach could be made to allow for other-than-whole-feet initial or returned values, as appropriate to your application.

Kent Cooper, AIA
0 Likes
Message 4 of 12

Anonymous
Not applicable

This is where I am now

 

(defun c:test (/ vpObj fldObj vpID util fldtxt)
  (vl-load-com)
  (setq vpObj  (vlax-ename->vla-object
   (car (nentsel "\nSelect the viewport: "))
        )
 fldObj (vlax-ename->vla-object (car (nentsel "\nSelect the Field: ")))
 vpID   (vla-get-objectid vpObj)
  );; setq
  (if (> (vl-string-search "x64" (getvar "platform")) 0)
    (progn
      (setq util (vla-get-Utility
     (vla-get-activedocument (vlax-get-acad-object))
   )
     vpID (vlax-invoke-method
     util
     "GetObjectIdString"
     vpObj
     :vlax-False
   )
      );; setq
    );; progn
    (setq vpID (vl-princ-to-string (vla-get-Objectid vpObj)))
  );; if
  (setq fldtxt (strcat "%<\\AcObjProp Object(%<\\_ObjId "
   vpID
   ">%).CustomScale \\f \"%lu2%ct1%ps\' %qf2816\">%"
  )
  )
  (vla-put-textstring fldObj (cond
   (( = fldtxt "10'") 5)
   ((= fldtxt "20'") 10)
   (T  "etc")))
  (vla-update fldObj)
  (vla-regen (vla-get-activedocument (vlax-get-acad-object))
      1
  )


  (princ)
);; test

 

 

 

Before adding the cond statement, the function gives me the scale of a viewport  and inserts it into a selected text field as 10', 20' and so on which is what i want. But i am creating a sceond lisp for now, that will output half the vp scale into the text field rather than the full scale. The way it is set up above, it will only give me the output "etc"

0 Likes
Message 5 of 12

Anonymous
Not applicable

This was the only way it would output anything other than nil, but it is only outputing 0.00000.. Not sure what is going on

 

 

(setq fldtxt (strcat "%<\\AcObjProp Object(%<\\_ObjId "
   vpID
   ">%).CustomScale \\f \"%lu2%ct1%ps\' %qf2816\">%"
  )
  )
  (vla-put-textstring fldObj (rtos (/ (atof "fldtxt") 2) 2 4) )
  (vla-update fldObj)
  (vla-regen (vla-get-activedocument (vlax-get-acad-object))
      1
  )


  (princ)
);; test

0 Likes
Message 6 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....
  (vla-put-textstring fldObj (cond
   (( = fldtxt "10'") 5)
   ((= fldtxt "20'") 10)
   (T  "etc")))

....

.... The way it is set up above, it will only give me the output "etc"


You need to supply a string, not a number, to something that's putting a string value into something:

 

....
  (vla-put-textstring fldObj (cond
   ((= fldtxt "10'") "5'")
   ((= fldtxt "20'") "10'")
   (T "etc")))

....

 

[It looks like dbroad didn't catch the quotes and foot marks.]

 

However, that may not be the only issue.  If it's putting "etc" into it, apparently that means the fldtxt variable does not contain either "10'" or "20'" -- if it contained one of those, you should be getting an error message about 5 or 10 not being a string.  Only if it doesn't contain either of those should it be proceeding to the none-of-the-above condition and returning "etc".  I'm at my older-version location right now without Fields to try anything with, but you should be able to check whether what's actually being stored in that variable is correct, and what about the deriving of it is not being done right.

 

Kent Cooper, AIA
0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

This was the only way it would output anything other than nil, but it is only outputing 0.00000.. Not sure what is going on

....
  (vla-put-textstring fldObj (rtos (/ (atof "fldtxt") 2) 2 4) )

....


That's quite a bit different from the code I suggested -- different function [(atof) rather than my (distof)], different mode and precision in (rtos).  What do you really want it to return, and what kind of input is it really getting?

 

Note this difference:

 

(atof "20'")

doesn't know what to think about the foot mark, so it abandons it and returns:
20.0

 

(distof "20'")

recognizes the meaning of the foot mark if you're set for Imperial units, and returns:
240.0

so that when that's divided in half and converted back in [Architectural-units mode (my 4) and to the nearest whole foot (my 0)] in (rtos), it becomes the right number of feet.

Kent Cooper, AIA
0 Likes
Message 8 of 12

Anonymous
Not applicable

When i put this in, im getting "bad argument type, error numberp nil

 

 

(defun c:test (/ vpObj fldObj vpID util fldtxt)
  (vl-load-com)
  (setq vpObj  (vlax-ename->vla-object
   (car (nentsel "\nSelect the viewport: "))
        )
 fldObj (vlax-ename->vla-object (car (nentsel "\nSelect the Field: ")))
 vpID   (vla-get-objectid vpObj)
  );; setq
  (if (> (vl-string-search "x64" (getvar "platform")) 0)
    (progn
      (setq util (vla-get-Utility
     (vla-get-activedocument (vlax-get-acad-object))
   )
     vpID (vlax-invoke-method
     util
     "GetObjectIdString"
     vpObj
     :vlax-False
   )
      );; setq
    );; progn
    (setq vpID (vl-princ-to-string (vla-get-Objectid vpObj)))
  );; if
  (setq fldtxt (strcat "%<\\AcObjProp Object(%<\\_ObjId "
   vpID
   ">%).CustomScale \\f \"%lu2%ct1%ps\' %qf2816\">%"
  )
  )
  (vla-put-textstring fldObj (rtos (/ (distof fldtxt) 2) 4 0))    >>originally it was just fldtxt
  (vla-update fldObj)
  (vla-regen (vla-get-activedocument (vlax-get-acad-object))
      1
  )
  (princ)
);; test

 

 

I put atof in just to see. Again, this is my first lisp and I am using most of these fuctions for the first time, so im not 100% sure as to what all is going on, so i have to learn by practice and trial & error. From what i am understanding though, it is only pulling a numerical value of 0 from fldtxt, but when i put it the original lisp, it will give me the value of the viewport scale as 20'. And in my understanding, fldtxt = the viewport scale.

 

 

In short, i need fldtxt to be half of what it originally gave me.

0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant

Change this line at the top:

 

(defun c:test (/ vpObj fldObj vpID util fldtxt)

 

to this:

 

(defun c:test (/ vpObj fldObj vpID util)

 

so that the fldtxt variable will not be "localized," and will therefore "survive" past the end of the routine.  Then run the routine, and afterwards, type this in at the Command: prompt:

 

!fldtxt

 

[the variable name preceded by an exclamation point].  That will tell you exactly what is going into that variable, which may be in some form that (distof) can't handle, so it's returning nil, which would explain the error message, presumably coming from the (/) function.  What does that return?

Kent Cooper, AIA
0 Likes
Message 10 of 12

dbroad
Mentor
Mentor

 

@Kent1Cooper

 

I was just trying to quickly show him the structure of the cond statement and didn't notice that he had asked for string returns. Thanks for clarifying it for him.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 11 of 12

Anonymous
Not applicable

This is the result..

 

Command: !fldtxt
"%<\\AcObjProp Object(%<\\_ObjId 8796088236208>%).CustomScale \\f \"%lu2%ct1%ps' %qf2816\">%"

0 Likes
Message 12 of 12

Anonymous
Not applicable

I figured it out,

 

I had to change .CustomScale \\f \"%lu2%ct1%ps\' %qf2816\">%"   to   CustomScale \\f \"%lu2%zs12%ct9[0.5]'\">%"

 

This is where i attempted to solve the problem before i posted and could not get it to work. I'm guessing that I was missing some character or something due to my inexperience.

 

But I appreciate the assistance from both of you, im sure I'll be getting more insight from you in the near future!

0 Likes