Author Topic: Why wont this routine return a value?  (Read 2072 times)

0 Members and 1 Guest are viewing this topic.

jbuzbee

  • Swamp Rat
  • Posts: 851
Why wont this routine return a value?
« on: April 08, 2014, 09:41:18 AM »
The following should return a value if the OpenDCL form isn't opened, but it doesn't.  What am I doing wrong?

Thank you

Code: [Select]
(defun kb:TextWidthValue  (/ rValue nValue ret)
  (cond ((dcl_Form_IsActive Annotation_Palette)
(Setq rValue (dcl_ComboBox_GetTBText Annotation_Palette_TextWidth)))
((dcl_Form_IsActive Annotation_Palette)
(Setq rValue (dcl_ComboBox_GetTBText Annotation_Palette_TextWidth))))
  (cond ((= (strcase rValue) "NARROW") (setq nValue 1.25))
((= (strcase rValue) "STANDARD") (setq nValue 2.5))
((= (strcase rValue) "EXTENDED") (setq nValue 3.75))
(t (setq nValue 4.0)))
  (setq ret (* nValue (kb:cannoscale)))
  ret)
James Buzbee
Windows 8

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Why wont this routine return a value?
« Reply #1 on: April 08, 2014, 10:45:46 AM »
It's a bit impossible to test without the rest of the code. But it seems as if it should at least return 4.0 x the CAnnoScale.

That is if there's no error in one of the routines called from that function. Note an error simply stops all Lisp. It should then list an error message onto the command line. Could you please copy that into a post?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Why wont this routine return a value?
« Reply #2 on: April 08, 2014, 10:51:51 AM »
Note: your first (cond) has the same condition twice. Maybe that is causing your confusion. Although the function (if called) should still return a value.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Why wont this routine return a value?
« Reply #3 on: April 08, 2014, 02:39:19 PM »
In the first two conditions, you define a variable rvalue.
In the remaining conditions and final expression, you define & reference the variable nvalue

Note that the final variable ret is not required.

EDIT: Oops! I see I have misread the code in my haste to spot the issue... :oops:
« Last Edit: April 08, 2014, 06:38:22 PM by Lee Mac »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Why wont this routine return a value?
« Reply #4 on: April 08, 2014, 05:15:57 PM »

James,
By my reading, the code as posted should return (* 4.0 (kb:cannoscale)) or error if kb:cannoscale has a value of nil.

The last line could be simply
Code - Auto/Visual Lisp: [Select]
  1. (* nValue (kb:cannoscale))


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Why wont this routine return a value?
« Reply #5 on: April 09, 2014, 10:59:36 AM »
Thanks all for the insight.  I still don't know why it won't return a value when rValue is nil.  :|
James Buzbee
Windows 8

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Why wont this routine return a value?
« Reply #6 on: April 09, 2014, 11:27:45 AM »
Ok I re-wrote the routine based on your suggestions and still no return.  :| :|


Code: [Select]
(defun kb:TextWidthValue  (/ rValue nValue ret)
  (setq nValue 2.5)
  (if(dcl_Form_IsActive Annotation_Palette)
(Setq rValue (dcl_ComboBox_GetTBText Annotation_Palette_TextWidth)))

  (cond ((= (strcase rValue) "NARROW") (setq nValue 1.25))
((= (strcase rValue) "STANDARD") (setq nValue 2.5))
((= (strcase rValue) "EXTENDED") (setq nValue 3.75))
;(t (setq nValue 2.5))
        )
  (* nValue (kb:cannoscale))
  )
James Buzbee
Windows 8

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Why wont this routine return a value?
« Reply #7 on: April 09, 2014, 11:33:01 AM »
This works:

Code: [Select]
(defun kb:TextWidthValue  (/ rValue nValue ret)
  (if
    (dcl_Form_IsActive Annotation_Palette)
(Setq rValue (dcl_ComboBox_GetTBText Annotation_Palette_TextWidth))
         (setq rValue "standard")
    )
  (cond ((= (strcase rValue) "NARROW") (setq nValue 1.25))
((= (strcase rValue) "STANDARD") (setq nValue 2.5))
((= (strcase rValue) "EXTENDED") (setq nValue 3.75))
)
  (* nValue (kb:cannoscale))
  )

Thanks again!!
James Buzbee
Windows 8

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Why wont this routine return a value?
« Reply #8 on: April 09, 2014, 02:39:13 PM »
    Thanks all for the insight.  I still don't know why it won't return a value when rValue is nil.  :|
    OK, going over the code again ... it would have been simple if you listed the error code as I asked in my 1st post on this thread:

    You'll note that your code isn't initializing the rValue variable in all cases. So if the dialog isn't visible then rValue = nil. Then the 1st test in the cond is using the strcase function. This will error if the argument contains anything but a string. So it errors because the value inside rValue is nil and not a string. Therefore once it reaches the first strcase call, the entire defun errors out - with something like:
    Quote
    bad argument type: stringp nil

    There are various ways you could go about fixing it:
    • Initialize rValue to the empty string "" (or any string whatsoever except one of the 3 options) at the same place you're initializing nValue (i.e. before the if, though anywhere in the defun's root scope before the cond would also suffice)
    • Set rValue in the else portion if the if (again empty or anything but the options). Similar to your last post
    • Add a condition in the cond before any of the strcase's which tests if the value inside rValue is a string. E.g. using (= (type rValue) 'STR); OR
    • Add a condition in the cond before any of the strcase's which tests if the value inside rValue not nil. E.g. using (not (null rValue))

    There might be other solutions also, but these are some I thought of.[/list]
    « Last Edit: April 09, 2014, 02:42:22 PM by irneb »
    Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.