Jump to content

Get External Data From a File


The Buzzard

Recommended Posts

It looks to me like you are not doing anything with the return of GetDimVariables - look at my example.

I was under the assumption it was to be used as I show here. Are you telling me this returns the values? This is not clear to me in your example.

 

  (cond
   ((= CLASS "150#") (setq FNAM (findfile "AF150.dim"))(GetDimVariables FNAM SLST)) 

Link to comment
Share on other sites

  • Replies 35
  • Created
  • Last Reply

Top Posters In This Topic

  • The Buzzard

    19

  • Lee Mac

    17

Top Posters In This Topic

Posted Images

Yes, look at the last return of the function. This is how the majority of subfunctions are engineered - to take input(s) and return output(s). :)

 

Look at my example in post #18.

Link to comment
Share on other sites

Sorry,

 

I must have missed that post, I was wondering about how much further I would need to go to separate the values, But that clears things up. Thanks pointing that out.

 

Looks like I have some fixes to make before I get to that.

Link to comment
Share on other sites

No worries :)

 

If I were you, I would make sure I understood the function completely, then you can use that knowledge to better your programming ability :)

Link to comment
Share on other sites

Ok I ran the Test.lsp from post 18.

I see that you concatenate the file name and I also see you are setting three variables (a b c). It does not seem like I have to add anything to this code to see it run, So I took out the variable localization of a b c and run the program.

 

It does not seem to do anything. When I check the variables, there are no values.

 

I even added

 (vl-load-com) 

and still nothing.

Link to comment
Share on other sites

Lee,

 

I managed to fix the code that I had to my liking. Although I did not use the code you provided I got quite a bit from this thread and some ideas you provided in the codes you posted.

 

1. I fixed the issue you mentioned about the protected symbol.

2. I changed the comma character to "|" and provided extra spacing in the dim file. This makes it now easier to read.

3. I provided a conditional to look for the file name. If the file is missing the program goes to an alert letting the user know what file is missing. It then returns to the menu after clicking Ok allowing the user to exit the program without an embarrassing crash so they can remedy the problem. Even though this may seem petty, The method used provides good error trapping to inform the user of a problematic issue they could run in to.

 

I really appreciate your help on this as well as the push in the right direction.

 

As always Thanks a million.

The Buzzard

 

Below are the fixes:

 

AF_SFS local function

 ;
; Set Flange Selection Function.
;
; OD = Outside Dia.
; TH = Thickness for all flanges except Lap Joint
; TJ = Thickness for Lap Joint
; RD = Raised Face Dia.
; HD = Hub Dia.
; HK = Hub OD at conn. for Weld Neck
; NH = Number of Holes
; HR = Hole Radius
; BC = Bolt Circle
; B1 = Flange ID for Slip-On
; B2 = Flange ID for Weld Neck & Socket Weld
; B3 = Flange ID for Lap Joint
; B4 = Flange ID for Threaded
; FT = Raised Face Thickness
; L1 = Flange Length for Threaded, Slip-On & Socket Weld
; L2 = Flange Length for Weld Neck
; L3 = Flange Length for Lap Joint
; CE = Center Line Extention
;
(defun AF_SFS ()
 (AF_CPS)
 (setq SELECTION     (nth (atoi AF:FTYP) DATA1)
       FTYP          (car SELECTION)
       AF:CLASSES    (nth (atoi AF:CLASS)(cdr SELECTION))
       CLASS         (car AF:CLASSES)
       SIZE          (nth (atoi AF:SIZE)(cdr AF:CLASSES))
       VIEW           AF:VIEW
       DFLG           AF:DFLG
       FACE           AF:FACE
       DUNT           AF:DUNT
       SLST nil SIZE (strcat "*"SIZE)
       FNAM          (strcat "AF"CLASS".dim"))
 (cond 
   ((<= (setq FILE (findfile FNAM)) nil)
    (alert (strcat "\n"FNAM" file missing."))(AF_MF))
   (t (setq F    (open FILE "R")
            ITEM (read-line F))
      (while ITEM
        (if (= ITEM SIZE)
          (setq DATA2 (read-line F) ITEM nil)
          (setq ITEM  (read-line F))))
      (if DATA2
        (progn
          (setq MAXS (strlen DATA2) CNT 1 CHR 1)
          (while (< CNT MAXS)
            (if (/= "|" (substr DATA2 CNT 1))
            (setq CHR  (1+ CHR))
            (setq NUM  (atof (substr DATA2 (1+ (- CNT CHR)) CHR))
                  DLST (append DLST (list NUM)) CHR 1))
          (setq CNT (1+ CNT)))
          (setq NUM (atof (substr DATA2 (1+ (- CNT CHR))))        	
                DLST (append DLST (list NUM)))))
      (close F)))
 (mapcar 'set '(OD TH TJ RD HD HK NH HR BC B1 B2 B3 B4 FT L1 L2 L3 CE) DLST)
 (setq NH (rtos NH 2 0))
 (setq NH (atoi NH))
 (cond
   ((= AF:VIEW "FACE")(AF_CFV))
   ((= AF:VIEW "SIDE")(AF_CSV)))
 (AF_PL)
 (princ))
;
;///////////////////////////////////////////////////////////////////////////////////////// 

 

AF150#.dim

 ;
; 150# Flange Dimension List
;
; OD = Outside Dia.
; TH = Thickness for all flanges except Lap Joint
; TJ = Thickness for Lap Joint
; RD = Raised Face Dia.
; HD = Hub Dia.
; HK = Hub OD at conn. for Weld Neck
; NH = Number of Holes
; HR = Hole Radius
; BC = Bolt Circle
; B1 = Flange ID for Slip-On
; B2 = Flange ID for Weld Neck & Socket Weld
; B3 = Flange ID for Lap Joint
; B4 = Flange ID for Threaded
; FT = Raised Face Thickness
; L1 = Flange Length for Threaded, Slip-On & Socket Weld
; L2 = Flange Length for Weld Neck
; L3 = Flange Length for Lap Joint
; CE = Center Line Extention
;
;OD   |  TH  |  TJ  |  RD  |  HD  |  HK  |  NH |  HR  |  BC  |  B1  |  B2  |  B3  | B4  |   FT   |  L1  |  L2  |  L3  | CE
*1/2
3.50 | 0.38 | 0.44 | 1.38 | 1.19 | 0.84 | 4.0 | 0.31 | 2.38 | 0.88 | 0.62 | 0.90 | 0.5 | 0.0625 | 0.56 | 1.81 | 0.62 | 0.5 

Document1.JPG

Document2.JPG

Document3.JPG

Link to comment
Share on other sites

Oh I missed one variable set:

 

(defun c:test ( / class size a b c )

 (setq class "150#" size "*1/2")

 (if
   (mapcar (function set) '( a b c )
     (GetDimVariables
       (strcat "AF" (substr class 1 (1- (strlen class))) ".dim") size
     )
   )

   (mapcar (function print) (list a b c))
 )
 (princ)
)
     

(defun GetDimVariables ( file flag / _ReadNextifFoo StringParser l )

 (defun _ReadNextifFoo ( file foo) ;; Lee Mac

   (cond (  (not (setq f (open file "r"))) nil)

         (t (while (and (setq n (read-line f)) (not (foo n))))

            (setq n (read-line f))
            (close f)
         )
   )
   n
 )

 (defun StringParser ( str del ) ;; Lee Mac
 
   (if (setq pos (vl-string-search del str))
     (cons (substr str 1 pos)
           (StringParser (substr str (+ pos 1 (strlen del))) del))
     (list str)
   )
 )

 (mapcar (function distof)

   (cond (  (not [color=Red][b](setq file[/b][/color] (findfile file)[b][color=Red])[/color][/b]) nil)

         (  (setq l (_ReadNextifFoo file (lambda ( line ) (eq line flag))))

            (StringParser l ",")
         )
   )
 )
)

Link to comment
Share on other sites

By the way,

 

I'm not sure that I understand your logic here:

 

(<= (setq FILE (findfile FNAM)) nil)

 

If the symbol FILE is successfully bound to a value it will be in a sense greater than nil, else it will be equal to nil. But at no point will it ever be less than nil.

 

In my opinion the inequality operators are better served for use with numerical arguments, I would use boolean operators like OR,AND,NOT for a validity check.

Link to comment
Share on other sites

By the way,

 

I'm not sure that I understand your logic here:

 

(<= (setq FILE (findfile FNAM)) nil)

 

If the symbol FILE is successfully bound to a value it will be in a sense greater than nil, else it will be equal to nil. But at no point will it ever be less than nil.

 

In my opinion the inequality operators are better served for use with numerical arguments, I would use boolean operators like OR,AND,NOT for a validity check.

Actually I tried that and had a problem with it. Who knows, I may have got ahead of myself at those times and forgot to do something else needed. I will look in to that, But for now it seems to be alright.

 

With regard to the missing variable set... Now he tells me! :(

Link to comment
Share on other sites

Just test all four and heres what I get:

 

not - Error: too many arguments when OK is selected

or - will post the ALERT if the file exists then give Error: bad argument type on exit

and - Error: bad argument type, stringp nil if file is missing

 

I guess it does not figure, But I cannot argue with the fact that

Link to comment
Share on other sites

Think about what you are doing - don't just try a bunch of different functions...

 

You are testing for a null value, so why use OR or AND...

 

Just

 

(cond (  (not (setq file (findfile <filename>)))

        (princ "\n** File not Found **")
     )
     (t
        ...

 

I use the very same construct in the example I provided...

 

As I said:

 

If I were you, I would make sure I understood the function completely, then you can use that knowledge to better your programming ability

Link to comment
Share on other sites

Think about what you are doing - don't just try a bunch of different functions...

 

You are testing for a null value, so why use OR or AND...

 

Just

 

(cond (  (not (setq file (findfile <filename>)))

        (princ "\n** File not Found **")
     )
     (t
        ...

 

I use the very same construct in the example I provided...

 

As I said:

Ok, I just tried that with the ALERT and it works. I just had to remove the "nil)" from it.

 

When I tested it, I had that in there. Thats where the problem was.

 

 

Thanks again.

Link to comment
Share on other sites

Lee,

 

I went back to your Test.lsp and this is what you had:

(cond (  (not (findfile file)) nil)  

 

With that in my code mixed with not and the alert did not work. My first impression would be that "nil)" was required.

 

Dammed if you do and Dammed if you don't.

 

 

Tested again and I get this Error on exit: ; error: bad argument type: numberp: nil

Link to comment
Share on other sites

I closed out the conditional at the point where my loop function starts up. I had the conditional closed out at the end of (close F), I believe thats where the error was being created.

 

No more error now.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...